#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2017-08-16 18:27:59
# Project: miaopai
from selenium import webdriver
import time
import requests
import config as cf
from pyspider.libs.base_handler import *
import pymysql
import redis
import db_action
import json

# plat_id 14
headers = {
    "Accept":"application/json, text/plain, */*",
    "Accept-Encoding":"gzip, deflate",
    "Accept-Language":"zh-CN,zh;q=0.8",
    "Content-Length":"15",
    "Content-Type":"application/x-www-form-urlencoded",
    "Host":"creator.miaopai.com",
    "Origin":"http://creator.miaopai.com",
    "Proxy-Connection":"keep-alive",
    "Referer":"http://creator.miaopai.com/video",
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36",
    "X-Requested-With":"XMLHttpRequest",
}


class Handler(BaseHandler):
    crawl_config = {
    }
    def __init__(self):
        conn = pymysql.connect(**cf.mysql_option)
        cursor = conn.cursor()
        r = redis.StrictRedis(**cf.redis)
        b = webdriver.PhantomJS()
        self.conn = conn
        self.cursor = cursor
        self.r = r
        self.b = b


    def login(self,user_info):
        try:
            self.b.get("http://creator.miaopai.com/login")
            time.sleep(2)
            input_ = self.b.find_element_by_xpath('//*[@id="app"]/div/span/main/main/section[1]/div/div[4]/form/div[1]/input')
            pw = self.b.find_element_by_xpath('//*[@id="app"]/div/span/main/main/section[1]/div/div[4]/form/div[2]/input')
            input_.send_keys(user_info['account_login_name'])
            pw.send_keys(user_info['account_login_password'])
            self.b.find_element_by_xpath('//*[@id="app"]/div/span/main/main/section[1]/div/div[4]/form/button/span').click()
            time.sleep(2)
            cookie = {}
            for i in self.b.get_cookies():
                cookie[i['name']] = i['value']
            res = requests.post('http://creator.miaopai.com/video/getList', data={
                "page": 1,
                "count": 20
            }, cookies=cookie, headers=headers)
            info = json.loads(res.text)['data']
            total = info['total']
            per_page = 500
            for j in range(total // per_page):
                res = requests.post('http://creator.miaopai.com/video/getList', data={
                            "page": j,
                            "count": 250
                        }, cookies=cookie, headers=headers)
                info = json.loads(res.text)
                data = info['data']['list']
                for video_info in data:
                    db_action.store_info_miaopai(video_info,user_info)
        except Exception as e:
            print(e)
            return False
        return True


    @every(minutes=4 * 60)
    def on_start(self):
        self.cursor.execute("select * from qp.med_plat_account WHERE plat_id = 7;")
        accounts = self.cursor.fetchall()
        for i in accounts:
            self.login(i)
        return {
            "task":"秒拍",
            "message":"done"
        }





b = webdriver.PhantomJS()

b.get("http://creator.miaopai.com/login")
time.sleep(2)
input_ = b.find_element_by_xpath('//*[@id="app"]/div/span/main/main/section[1]/div/div[4]/form/div[1]/input')
pw = b.find_element_by_xpath('//*[@id="app"]/div/span/main/main/section[1]/div/div[4]/form/div[2]/input')
input_.send_keys("18637616572")
pw.send_keys("maxfocuS3503")
b.find_element_by_xpath('//*[@id="app"]/div/span/main/main/section[1]/div/div[4]/form/button/span').click()
time.sleep(2)
print(b.find_element_by_tag_name('body').text)


cookie = {}
for i in b.get_cookies():
    cookie[i['name']] = i['value']
print(cookie)
res= requests.post('http://creator.miaopai.com/video/getList',data={
    "page":1,
    "count":100
},cookies=cookie,headers=headers)
import json
info = json.loads(res.text)['data']
total= info['total']
print(total)
res= requests.post('http://creator.miaopai.com/video/getList',data={
    "page":1,
    "count":500
},cookies=cookie,headers=headers,timeout=100)
info = json.loads(res.text)
data = info['data']
print(data)
print(res.text)
print(len(info['data']['list']))
print(info['data']['list'][21])


{'SRV_CREATOR_COOKIE_LOGIN_TOKEN': 'rretG568rg8%7EKCWMEmCVnjD2VaXHwfIx',
 'Hm_lpvt_77106d46af46cb21eedfa2a9da870cb4': '1502878250',
 'Hm_lvt_77106d46af46cb21eedfa2a9da870cb4': '1502877609,1502877652,1502878197,1502878250',
 'PHPSESSID': '0oa91a6vc14d7ofu0muprmi6q3'}
#
# PHPSESSID=ogmcidbhd9je4t59n3tg1i4vj4;
# Hm_lvt_e8fa5926bca558076246d7fb7ca12071=1502871866;1502871866
# Hm_lpvt_e8fa5926bca558076246d7fb7ca12071=1502871866;
# Hm_lvt_77106d46af46cb21eedfa2a9da870cb4=1502871888,1502874644,1502874648,1502874654;
# Hm_lpvt_77106d46af46cb21eedfa2a9da870cb4=1502874751;
# SRV_CREATOR_COOKIE_LOGIN_TOKEN=rretG568rg8%7EKCWMEmCVnjD2VaXHwfIx
{'Hm_lvt_e8fa5926bca558076246d7fb7ca12071': '1502871866',
 'Hm_lpvt_e8fa5926bca558076246d7fb7ca12071': '1502871866',
 'SRV_CREATOR_COOKIE_LOGIN_TOKEN': 'rretG568rg8%7EKCWMEmCVnjD2VaXHwfIx',
 'Hm_lpvt_77106d46af46cb21eedfa2a9da870cb4': '1502878665',
 'Hm_lvt_77106d46af46cb21eedfa2a9da870cb4': '1502877652,1502878197,1502878250,1502878665',
 'PHPSESSID': 'umtrb23kh9fsmjdv2talui3pl5'}

